-- BarTool
-- 工具栏
BarTool = class()

function BarTool:init()
    self.s = 30
    self.buttons = {}
    self.pointCount = 5
    self.pressedPos = nil
    self.touchid = 0 
    self.bag = BackBag()
    self:setButton(1,"Project:bagS",function() game.gui.backBag:changeState() end)
    --self:setButton(2,"Project:bagS",function() move:changeOpen() end)
    --
end

function BarTool:draw()
    spriteMode(CENTER)
    for p = 1, self.pointCount do
        if self.buttons[p] then
            sprite("Project:seletorA",45, (-25 + 80 * p), 75, 75)
            sprite(self.buttons[p].tex, 45, (-25 + 80 * p), (80 - self.s), (80 - self.s))
        end
    end
end

function BarTool:touched(touch)
    for p = 1,self.pointCount do
        if self:isTouchInPos(touch,p) then
            if touch.state == BEGAN and self.touchid == 0 then
                self.pressedPos = p
                self.touchid = touch.id
            elseif touch.state == ENDED and touch.id == self.touchid then
                if self.buttons[p] then
                    self.buttons[p]:func()
                end
                self.pressedPos = nil
                self.touchid = 0
            end
            return true
        end
    end
    if touch.id == self.touchid and touch.state == ENDED then
        self.touchid = 0
        self.pressedPos = nil
        return true
    end
    return false
end

function BarTool:isTouchInPos(touch,pos)
    return touch.x > math.abs((45 - 40)) and touch.x < math.abs((45 + 40)) and touch.y > math.abs((-25 + 80 * pos) - 40) and touch.y < math.abs((-25 + 80 * pos) + 40)
end

function BarTool:setButton(pos,texture,func)
    self.buttons[pos] = {tex = texture,func = func}
end


